home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 361_01 / mouse.c < prev    next >
C/C++ Source or Header  |  1991-09-18  |  5KB  |  172 lines

  1.  
  2. /* These Interfaces to the Mouse Driver Interrupt 0x33h will function
  3.  * in both Text and Graphics Modes.
  4.  *
  5.  * Note: You MUST Link w/ Graphics.Lib ON even for text-only use.
  6.  *
  7.  * Author: J.Ekwall                                     13 September 91
  8.  *
  9.  * Copyrighted to the Public Domain.  Unlimited Distribution Authorized.
  10.  *
  11.  * User Assumes All Risks/Liabilities.
  12.  *
  13.  * Last Update: 16 September 91/EK
  14.  */
  15.  
  16. #include <dos.h>
  17. #include <graphics.h>
  18. #include <gadgets.h>
  19. #include <graphics.h>
  20. #include <mouse.h>
  21. #include <stdek.h>
  22.  
  23. static _Mouse = 0;
  24.  
  25. static int Handles(int N);
  26.  
  27. static int Handles(int N)
  28. {
  29.     int i;
  30.  
  31.     if (N < 0)                  /* Button Status to Character */
  32.        return "┼║╣╢╕¬¥"[7+N] & 0xFF;
  33.     else {                      /*      Character to Button Handle */
  34.        for (i = 0; ("¥¬╢"[i] & 0xFF) != N && i++ < 2; );
  35.        return i;
  36.     }
  37. }
  38.        
  39. void Mouse(int *A, int *B, int *C, int *D)
  40. {
  41.     union REGS rg;
  42.  
  43.     rg.x.ax = *A; rg.x.bx = *B; rg.x.cx = *C; rg.x.dx = *D;
  44.     int86(0x33, &rg, &rg);
  45.     *A = rg.x.ax; *B = rg.x.bx; *C = rg.x.cx; *D = rg.x.dx;
  46. }
  47.  
  48. int MouseAtxy(int *X, int *Y)
  49. {
  50.     union REGS rg;
  51.  
  52.     if (!MouseOK()) return 0;
  53.     rg.x.ax = 3; int86(0x33, &rg, &rg); *X = rg.x.cx; *Y = rg.x.dx;
  54.     if (IsTextMode()) { *X = (*X) / 8 + 1; *Y = (*Y) / 8 + 1; }
  55.     else if (getmaxx() IS 319) (*X) /= 2;
  56.     return (rg.x.bx) ? Handles(-rg.x.bx) : 0;
  57. }
  58.  
  59. int MouseClicked(int Button)
  60. {
  61.     int i, n = 0;
  62.     union REGS rg;
  63.  
  64.     if (! _Mouse) return 0; i = Handles(Button);
  65.     do { int86(0x28, &rg, &rg); rg.x.ax = 3; int86(0x33, &rg, &rg);
  66.     }  while (rg.x.bx);
  67.     if (i < 3) {
  68.        rg.x.ax = 6; rg.x.bx = i; int86(0x33, &rg, &rg); n = (rg.x.bx > 0);
  69.     } else while (i--) {
  70.        n *= 2; rg.x.ax = 6; rg.x.bx = i; int86(0x33, &rg, &rg);
  71.        n -= (rg.x.bx > 0);
  72.     }
  73.     return (n < 0) ? Handles(n) : n;
  74. }
  75.  
  76. int MouseClickedxy(int Button, int *X, int *Y)
  77. {
  78.     union REGS rg;
  79.     
  80.     if (!MouseOK() || (rg.x.bx = Handles(Button)) IS 3) return 0;
  81.     rg.x.ax = 6; int86(0x33, &rg, &rg);
  82.     *X = rg.x.cx; *Y = rg.x.dx;
  83.     if (IsTextMode()) { *X = (*X) / 8 + 1; *Y = (*Y) / 8 + 1; }
  84.     else if (getmaxx() IS 319) (*X) /= 2;
  85.     return (rg.x.bx) ? Handles(-rg.x.bx) : 0;
  86. }
  87.  
  88. void MouseDeltaxy(int *X, int *Y)
  89. {
  90.     int Dx, Dy;
  91.     union REGS rg;
  92.  
  93.     if (!MouseOK()) { *X = *Y = 0; return; }
  94.     rg.x.ax = 0x1B; int86(0x33, &rg, &rg); Dx = rg.x.bx; Dy = rg.x.cx;
  95.     rg.x.ax = 0x0B; int86(0x33, &rg, &rg); 
  96.     *X = (rg.x.cx * Dx) / 8; *Y = (rg.x.dx * Dy) / 8; 
  97.     if (IsTextMode()) { *X = (*X) / 8 + 1; *Y = (*Y) / 8 + 1; }
  98.     else if (getmaxx() IS 319) (*X) /= 2;
  99. }
  100.  
  101. int MouseOK(void) { return _Mouse; }
  102.  
  103. void MousePageSet(int Which1)
  104. {
  105.       union REGS rg;
  106.       
  107.       rg.x.ax = 0x1D; rg.x.bx = Which1; int86(0x33, &rg, &rg);
  108. }
  109.  
  110. int MouseReSet(void)
  111. /*  Resets the mouse.  Returns # of Buttons if Present or 0 if No Mouse. */
  112. {
  113.     union REGS rg;
  114.  
  115.     rg.x.ax = 0; int86(0x33, &rg, &rg); if (rg.x.ax IS 0) return 0;
  116.     _Mouse = 1; MouseSwitch(-1);
  117.     return (rg.x.bx IS 0) ? 2 : (rg.x.bx IS 3) ? 3 : 1;
  118. }
  119.  
  120. void MouseSpeed(int Dx, int Dy, int Zoom)
  121. {
  122.       union REGS rg;
  123.       
  124.       rg.x.ax = 0x1A; rg.x.bx = Dx; rg.x.cx = Dy; rg.x.dx = Zoom;
  125.       int86(0x33, &rg, &rg);
  126. }
  127.  
  128. int MouseSwitch(int Flag)
  129. {
  130.     union REGS rg;
  131.     static int Cd_mouse = 1;
  132.  
  133.     if (Flag IS -1) Cd_mouse = 0;
  134.     if (!Flag && !Cd_mouse) return 0;
  135.     if (Flag && Cd_mouse) return 1;
  136.     rg.x.ax = 1 + !Flag; int86(0x33, &rg, &rg); Cd_mouse = !Cd_mouse;
  137.     return !Cd_mouse;
  138. }
  139.  
  140. void MouseToxy(int X, int Y)
  141. {
  142.     union REGS rg;
  143.     
  144.     if (!MouseOK()) { if (IsTextMode()) Gotoxy(X, Y); return; }
  145.     if (IsTextMode()) { rg.x.cx = --X * 8; rg.x.dx = --Y * 8; }
  146.     else {
  147.        if (getmaxx() == 319) X *= 2;    /*      Adjust for virtual coordinates */
  148.        rg.x.cx = X; rg.x.dx = Y; 
  149.     }
  150.     rg.x.ax = 4; int86(0x33, &rg, &rg);
  151. }
  152.  
  153. int MouseTrap(int Left, int Top, int Right, int Bottom)
  154. {
  155.     int X, Y;
  156.     
  157.     if (MouseOK()) MouseAtxy(&X, &Y); else return 0;
  158.     return((X >= Left && X <= Right && Y >= Top && Y <= Bottom) ? 1 : 0);
  159. }
  160.  
  161. int MouseWait4User(void)
  162. {
  163.     int c, X, Y;
  164.  
  165.     if (IsTextMode()) { Getxy(&X, &Y); MouseToxy(X, Y); }
  166.     for (c = 0; !c; )
  167.        if (!(c = Kbq_poll()) && !(c = MouseClicked(BTN_ANY)) &&
  168.           IsTextMode()) { MouseAtxy(&X, &Y); Gotoxy(X, Y); }
  169.     return c;
  170. }
  171.  
  172.